# -*- coding: utf-8 -*-
import json
from urllib import parse

import scrapy

from spider.items import ProductItem


class TaobaoSpider(scrapy.Spider):
    name = 'tb'
    allowed_domains = ['s.taobao.com', ]

    search_url = "https://s.taobao.com/search?q=%E6%B2%90%E6%B5%B4%E9%9C%B2&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20190116&ie=utf8"
    first_url = "https://search.jd.com/Search?keyword={}&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&stock=1&page={}&s={}&click=0"
    next_url = "https://search.jd.com/s_new.php?keyword={}&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&bs=1&wq={}stock=1&" \
               "page={}&s={}&scrolling=y&log_id=1545637088.54177&tpl=3_M"
    detail_url = "https://item.jd.com/{id}.html"
    price_url = "https://p.3.cn/prices/mgets?pduid=15382152254501857071747&skuIds=J_{id}"

    def __init__(self, name):
        self.q = name

    def start_requests(self):
        # yield: 既可以传出一个item到pipeline进行加工，
        # 也可以传出一个新的Request请求。在传出一个新请求的时候，就会多开启一个线程，Scrapy是异步多线程的爬虫框架
        headers = {
            'authority': 's.taobao.com',
            'method': 'GET',
            'path': '/search?q=%E6%B2%90%E6%B5%B4%E9%9C%B2&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20190116&ie=utf8',
            'scheme': 'https',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cookie': 't=8250318c20ac80dfcf664ff6ac63c79b; cookie2=14ff11929a59ab5d6cda5700a188ec25; v=0; _tb_token_=e3b9ab381b37b; cna=tYjFFModZU0CAToUHljsdNb4; hng=CN%7Czh-CN%7CCNY%7C156; thw=cn; unb=2200613671142; sg=%E5%8D%A124; _l_g_=Ug%3D%3D; skt=7408626449e3071f; cookie1=B0aobIG2OKxDseIyYipctjQRFINr7KDssN9N2iXgupM%3D; csg=fc1d5cde; uc3=vt3=F8dByE%2Bn%2B4KMYhAiFLM%3D&id2=UUphyu%2BFuvOFdOD%2FDQ%3D%3D&nk2=UUpicfDClPldtw%3D%3D&lg2=U%2BGCWk%2F75gdr5Q%3D%3D; existShop=MTU0NzYyMDI2Nw%3D%3D; tracknick=2233%5Cu6D4B%5Cu8BD5%5Cu5361; lgc=2233%5Cu6D4B%5Cu8BD5%5Cu5361; _cc_=W5iHLLyFfA%3D%3D; dnk=2233%5Cu6D4B%5Cu8BD5%5Cu5361; _nk_=2233%5Cu6D4B%5Cu8BD5%5Cu5361; cookie17=UUphyu%2BFuvOFdOD%2FDQ%3D%3D; tg=4; l=AmRk1hZ32BVggSOTZ-J8oW2rtGlWWohn; enc=PlxorL9jzdzPJFPOYakGtc595K9AqRB9%2B86485CvaNN%2FRc%2BOveTFKq%2BUbYQR94ioezLV9shyJiGTl1uNJ8HAWg%3D%3D; JSESSIONID=387CF896369A644DEE465E3D1970FEAA; uc1=cookie16=Vq8l%2BKCLySLZMFWHxqs8fwqnEw%3D%3D&cookie21=U%2BGCWk%2F7oPIg&cookie15=W5iHLLyFOGW7aA%3D%3D&existShop=false&pas=0&cookie14=UoTYMbm37AEH8Q%3D%3D&tag=8&lng=zh_CN; mt=ci=0_1; swfstore=138835; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; whl=-1%260%260%261547620287991; isg=BPDwK_pmWM1pzQTJVbrLyAg5wb5zQrjpmZVhEOpAvcsepZNPkky-ExTX-e1gNYxb',
            'referer': 'https://s.taobao.com/search?q=%E6%B2%90%E6%B5%B4%E9%9C%B2&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20190116&ie=utf8',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
        }
        yield scrapy.Request(self.search_url, callback=self.parse_page, headers=headers)

    def parse_page(self, response):
        print(response.text)
